package com.chanceit.ssss.activity;

import org.gl.customview.ViewPager;
import org.gl.customview.viewpager.PagerSlidingTabStrip;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.TypedValue;

import com.chanceit.ssss.adapters.SafeTabPagerAdapter;
import com.chanceit.ssss.common.IntentExtraDataKey;
import com.chanceit.ssss.data.DataSources;
import com.chanceit.ssss.fragments.CarHitListFragment;

public class SafeActivity extends BaseFragmentActivity {
	SafeTabPagerAdapter mSectionsPagerAdapter;
	ViewPager mViewPager;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_safe);
		mSectionsPagerAdapter = new SafeTabPagerAdapter(
				getSupportFragmentManager(), this);

		mViewPager = (ViewPager) findViewById(R.id.pager);
		mViewPager.setAdapter(mSectionsPagerAdapter);
		int selectIndex = 0;
		if (DataSources.BusinessType_birthday.equals(getIntent()
				.getStringExtra(IntentExtraDataKey.BUSINESS_TYPE))) {
			selectIndex = mSectionsPagerAdapter.getCount() - 1;

		}
		if (selectIndex > mSectionsPagerAdapter.getCount() || selectIndex < 0) {
			selectIndex = 0;
		}
		mViewPager.setCurrentItem(selectIndex);
		final int pageMargin = (int) TypedValue.applyDimension(
				TypedValue.COMPLEX_UNIT_DIP, 4, getResources()
						.getDisplayMetrics());
		mViewPager.setPageMargin(pageMargin);

		PagerSlidingTabStrip tabs = (PagerSlidingTabStrip) findViewById(R.id.tabs);
		if (tabs != null) {
			tabs.setViewPager(mViewPager);
			tabs.setIndicatorColor(0xFF3F9FE0);
		}
		if (findViewById(R.id.fragment_container) != null) {
			if (savedInstanceState != null) {
				return;
			}
		}
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if (requestCode == CarHitListFragment.QUESTCODE_HIT
				&& resultCode == RESULT_OK) {
			Fragment fra = mSectionsPagerAdapter.getFagments()[mViewPager
					.getCurrentItem()];
			if (fra!=null&&fra instanceof CarHitListFragment) {
				((CarHitListFragment) fra).receiveMessage(null);
			}
		}
	}
	// @Override
	// public void onBackPressed() {
	// GlobalVariable.getGlobalInstance().intent.putExtra(IntentExtraDataKey.BUSINESS_TYPE,"unknow");
	// super.onBackPressed();
	// }
}
